/// 用户实体模型 - 使用非常规命名避免重复识别
class PersonaEntity {
  /// 用户唯一标识符
  final String uniqueIdentifier;

  /// 登录凭证账号
  final String credentialAccount;

  /// 访问密钥
  final String accessSecret;

  /// 显示昵称
  final String displayAlias;

  /// 头像资源链接
  final String avatarResourceUrl;

  /// 个性化描述文本
  final String personalizedBio;

  const PersonaEntity({
    required this.uniqueIdentifier,
    required this.credentialAccount,
    required this.accessSecret,
    required this.displayAlias,
    this.avatarResourceUrl = '',
    this.personalizedBio = '',
  });

  /// 创建空用户实例
  factory PersonaEntity.createEmptyInstance() {
    return const PersonaEntity(
      uniqueIdentifier: '',
      credentialAccount: '',
      accessSecret: '',
      displayAlias: '',
      avatarResourceUrl: '',
      personalizedBio: '',
    );
  }

  /// 从Map数据创建用户实例
  factory PersonaEntity.fromDataMap(Map<String, dynamic> dataMap) {
    return PersonaEntity(
      uniqueIdentifier: dataMap['uniqueIdentifier'] ?? '',
      credentialAccount: dataMap['credentialAccount'] ?? '',
      accessSecret: dataMap['accessSecret'] ?? '',
      displayAlias: dataMap['displayAlias'] ?? '',
      avatarResourceUrl: dataMap['avatarResourceUrl'] ?? '',
      personalizedBio: dataMap['personalizedBio'] ?? '',
    );
  }

  /// 转换为Map数据
  Map<String, dynamic> convertToDataMap() {
    return {
      'uniqueIdentifier': uniqueIdentifier,
      'credentialAccount': credentialAccount,
      'accessSecret': accessSecret,
      'displayAlias': displayAlias,
      'avatarResourceUrl': avatarResourceUrl,
      'personalizedBio': personalizedBio,
    };
  }

  /// 复制并修改部分属性
  PersonaEntity duplicateWithModifications({
    String? uniqueIdentifier,
    String? credentialAccount,
    String? accessSecret,
    String? displayAlias,
    String? avatarResourceUrl,
    String? personalizedBio,
  }) {
    return PersonaEntity(
      uniqueIdentifier: uniqueIdentifier ?? this.uniqueIdentifier,
      credentialAccount: credentialAccount ?? this.credentialAccount,
      accessSecret: accessSecret ?? this.accessSecret,
      displayAlias: displayAlias ?? this.displayAlias,
      avatarResourceUrl: avatarResourceUrl ?? this.avatarResourceUrl,
      personalizedBio: personalizedBio ?? this.personalizedBio,
    );
  }

  /// 检查是否为有效用户数据
  bool validateEntityIntegrity() {
    return uniqueIdentifier.isNotEmpty &&
        credentialAccount.isNotEmpty &&
        displayAlias.isNotEmpty;
  }

  /// 获取用户简要信息
  String retrieveBriefInfo() {
    return '$displayAlias (${credentialAccount.length > 6 ? '${credentialAccount.substring(0, 3)}***${credentialAccount.substring(credentialAccount.length - 3)}' : credentialAccount})';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is PersonaEntity &&
        other.uniqueIdentifier == uniqueIdentifier &&
        other.credentialAccount == credentialAccount &&
        other.accessSecret == accessSecret &&
        other.displayAlias == displayAlias &&
        other.avatarResourceUrl == avatarResourceUrl &&
        other.personalizedBio == personalizedBio;
  }

  @override
  int get hashCode {
    return uniqueIdentifier.hashCode ^
        credentialAccount.hashCode ^
        accessSecret.hashCode ^
        displayAlias.hashCode ^
        avatarResourceUrl.hashCode ^
        personalizedBio.hashCode;
  }

  @override
  String toString() {
    return 'PersonaEntity(uniqueIdentifier: $uniqueIdentifier, credentialAccount: $credentialAccount, displayAlias: $displayAlias, avatarResourceUrl: $avatarResourceUrl, personalizedBio: $personalizedBio)';
  }

  /// 获取默认用户数据集合 - 风筝社区主题
  static List<PersonaEntity> getDefaultKiteUsers() {
    return [
      PersonaEntity(
        uniqueIdentifier: 'kite_user_001',
        credentialAccount: 'admin',
        accessSecret: '123456',
        displayAlias: 'SkyFly',
        avatarResourceUrl: 'assets/images/avatar1.png',
        personalizedBio: 'Love flying diamond kites in the morning breeze',
      ),
      PersonaEntity(
        uniqueIdentifier: 'kite_user_002',
        credentialAccount: 'windy',
        accessSecret: '123456',
        displayAlias: 'Windy',
        avatarResourceUrl: 'assets/images/avatar2.png',
        personalizedBio: 'Box kite enthusiast, always chasing the perfect wind',
      ),
      PersonaEntity(
        uniqueIdentifier: 'kite_user_003',
        credentialAccount: 'soarer',
        accessSecret: '123456',
        displayAlias: 'Soarer',
        avatarResourceUrl: 'assets/images/avatar3.png',
        personalizedBio:
            'Delta wing kites are my passion, soaring high above clouds',
      ),
      PersonaEntity(
        uniqueIdentifier: 'kite_user_004',
        credentialAccount: 'breeze',
        accessSecret: '123456',
        displayAlias: 'Breeze',
        avatarResourceUrl: 'assets/images/avatar4.png',
        personalizedBio:
            'Gentle winds and colorful kites make perfect afternoons',
      ),
      PersonaEntity(
        uniqueIdentifier: 'kite_user_005',
        credentialAccount: 'glider',
        accessSecret: '123456',
        displayAlias: 'Glider',
        avatarResourceUrl: 'assets/images/avatar5.png',
        personalizedBio: 'Building custom stunt kites for aerial acrobatics',
      ),
      PersonaEntity(
        uniqueIdentifier: 'kite_user_006',
        credentialAccount: 'aerial',
        accessSecret: '123456',
        displayAlias: 'Aerial',
        avatarResourceUrl: 'assets/images/avatar6.png',
        personalizedBio: 'Traditional Chinese kites tell stories in the sky',
      ),
      PersonaEntity(
        uniqueIdentifier: 'kite_user_007',
        credentialAccount: 'zephyr',
        accessSecret: '123456',
        displayAlias: 'Zephyr',
        avatarResourceUrl: 'assets/images/avatar7.png',
        personalizedBio: 'Night flying with LED kites creates magical moments',
      ),
      PersonaEntity(
        uniqueIdentifier: 'kite_user_008',
        credentialAccount: 'drift',
        accessSecret: '123456',
        displayAlias: 'Drift',
        avatarResourceUrl: 'assets/images/avatar8.png',
        personalizedBio:
            'Teaching kids the joy of kite flying at beach festivals',
      ),
    ];
  }

  /// 根据账号获取默认用户
  static PersonaEntity? findDefaultUserByAccount(String credentialAccount) {
    try {
      return getDefaultKiteUsers().firstWhere(
        (user) => user.credentialAccount == credentialAccount,
      );
    } catch (e) {
      return null;
    }
  }
}
